﻿using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper.SqlServer;
using ZilLion.Service.TaskPlatform.Domain.Entity;
using ZilLion.Service.TaskPlatform.Domain.Respository.Interface;

namespace ZilLion.Service.TaskPlatform.Domain.Respository.Sqlserver
{
    public class TaskConfigRespository : SqlServerRespository<TaskConfig>, ITaskConfigRespository
    {
        public TaskConfigRespository()
        {
            Context = new TaskRunnerSqlserverContext();
        }


        public IList<TaskConfig> GetjobConfigs()
        {
            return GetList(@"select * from task_config where isdeleted=0").ToList();
        }


        /// <summary>
        ///     删除
        /// </summary>
        /// <param name="config"></param>
        public void RemoveTaskConfig(TaskConfig config)
        {
            config.IsDeleted = 1; //置状态
            Modify(config);
        }

        public void AddData(TaskConfig taskConfig)
        {
            Add(taskConfig);
        }

        public void MotifyData(TaskConfig taskConfig)
        {
            Modify(taskConfig);
        }


        /// <summary>
        ///     获取单条config
        /// </summary>
        /// <returns></returns>
        public TaskConfig GetjobConfigById(string jobid, string nodeid = null)
        {
            TaskConfig result;
            if (nodeid.IsNotNullOrEmpty())
                result = GetList("select * from task_config where taskid=@jobid and isdeleted=0 and nodeid=@nodeid",
                    new {jobid, nodeid}).FirstOrDefault();

            else
                result = GetList(@"select * from task_config where taskid=@jobid and isdeleted=0", new {jobid})
                    .FirstOrDefault();


            return result;
        }
    }
}